package com.sojoys.artifact.tools;

import java.io.File;
import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 生成protobuff代码
 * @author tom.guo
 * @date 2014-04-04
 */
public class ToolProto {
	
	/**protoc.exe 路径*/
	private static String DEFAULT_COMMAND = "D:/protoc-2.5.0-win32/protoc.exe";
	
	private static String DEFAULT_DESTPATH = "C:\\Users\\win7\\git\\bullfight\\bullfight-all\\bullfight-sala\\src\\main\\java";

	/**proto描述文件路径*/
	private static String DEFAULT_SRCTPATH_1 = "D:\\Work\\SVN\\新建文件夹\\Resource\\lua\\net\\protocol\\c2s";
	private static String DEFAULT_SRCTPATH_2 = "D:\\Work\\SVN\\新建文件夹\\Resource\\lua\\net\\protocol\\s2c";
	
	public static void main(String args[]) throws IOException{
		File c2sDir = new File(DEFAULT_SRCTPATH_1);
		File s2cDir = new File(DEFAULT_SRCTPATH_2);
		List<File> files = new ArrayList<File>();
		files.addAll(Arrays.asList(c2sDir.listFiles()));
		files.addAll(Arrays.asList(s2cDir.listFiles()));
		Date yestoday = new Date(System.currentTimeMillis()-24*60*60*1000);//24小时以前
		for(File descFile:files){
			String descFileName = descFile.getName();
			Date lastModifiedDate = new Date(descFile.lastModified());
			if(descFileName.endsWith("proto") && lastModifiedDate.after(yestoday)){
				if(descFileName.contains("c2s")){
					genCode(DEFAULT_SRCTPATH_1, DEFAULT_DESTPATH, descFileName);
				}
				if(descFileName.contains("s2c")){
					genCode(DEFAULT_SRCTPATH_2, DEFAULT_DESTPATH, descFileName);
				}
			}
		}
	
	}
	
	public static void genCode(String srcPath,String destPath,String descFile) throws IOException{
		String command = String.format("%s -I=%s --java_out=%s %s ", DEFAULT_COMMAND,srcPath,destPath,srcPath+"\\"+descFile);
		 Runtime.getRuntime().exec(command);
		 System.out.println(command);
	}
	
}
